package com.huizu.project.service.impl;

import java.util.Date;
import java.util.List;

import com.huizu.common.enums.LaborEnum;
import com.huizu.common.exception.ServiceException;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.*;
import com.huizu.project.mapper.ProFileMapper;
import com.huizu.project.mapper.ProLdFsMapper;
import com.huizu.project.mapper.ProUserMapper;
import com.huizu.project.service.IBurialPointService;
import com.huizu.project.service.IProLdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.mapper.ProLdzjMapper;
import com.huizu.project.service.IProLdzjService;

import static com.huizu.common.utils.PageUtils.startPage;

/**
 * 劳动总结报告Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Service
public class ProLdzjServiceImpl implements IProLdzjService 
{
    @Autowired
    private ProLdzjMapper proLdzjMapper;

    @Autowired
    private ProLdFsMapper proLdFsMapper;

    @Autowired
    private IProLdService iProLdService;

    @Autowired
    private ProFileMapper proFileMapper;

    @Autowired
    private ProUserMapper proUserMapper;

    @Autowired
    private IBurialPointService burialPointService;

    /**
     * 查询劳动总结报告
     * 
     * @param id 劳动总结报告主键
     * @return 劳动总结报告
     */
    @Override
    public ProLdzj selectProLdzjById(String id)
    {
        ProLdzj proLdzj = proLdzjMapper.selectProLdzjById(id);
        proLdzj.setProjectName("劳动总结");
        ProFile proFile = new ProFile();
        proFile.setBusinessId(id);
        proLdzj.setProFileList(proFileMapper.selectProFileList(proFile));
        return proLdzj;
    }

    /**
     * 查询劳动总结报告列表
     * 
     * @param proLdzj 劳动总结报告
     * @return 劳动总结报告
     */
    @Override
    public List<ProLdzj> selectProLdzjList(ProLdzj proLdzj)
    {
        startPage();
        return proLdzjMapper.selectProLdzjList(proLdzj);
    }

    /**
     * 新增劳动总结报告
     * 
     * @param proLdzj 劳动总结报告
     * @return 结果
     */
    @Override
    public int insertProLdzj(ProLdzj proLdzj)
    {
        proLdzj.setCreateTime(DateUtils.getNowDate());
        return proLdzjMapper.insertProLdzj(proLdzj);
    }

    /**
     * 修改劳动总结报告
     * 
     * @param proLdzj 劳动总结报告
     * @return 结果
     */
    @Override
    public int updateProLdzj(ProLdzj proLdzj)

    {
        proFileMapper.deleteProFileByBusinessId(proLdzj.getId());
        iProLdService.insertProFiles(proLdzj.getUserId(),proLdzj.getId(),"ldzj",proLdzj.getProFileList());
        return proLdzjMapper.updateProLdzj(proLdzj);
    }

    /**
     * 批量删除劳动总结报告
     * 
     * @param ids 需要删除的劳动总结报告主键
     * @return 结果
     */
    @Override
    public int deleteProLdzjByIds(String[] ids)
    {
        return proLdzjMapper.deleteProLdzjByIds(ids);
    }

    /**
     * 删除劳动总结报告信息
     * 
     * @param id 劳动总结报告主键
     * @return 结果
     */
    @Override
    public int deleteProLdzjById(String id)
    {
        return proLdzjMapper.deleteProLdzjById(id);
    }

    /**
     * 新增劳动总结报告
     *
     * @param proLdzj 劳动总结报告
     * @return 结果
     */
    public int insertProLdzjApi(ProLdzj proLdzj){
        proLdzj.setId(String.valueOf(SnowFlake.nextId()));
        proLdzj.setCreateTime(new Date());
        proLdzj.setStatus("1");
        iProLdService.insertProFiles(proLdzj.getUserId(),proLdzj.getId(),"ldzj",proLdzj.getProFileList());
        int row = proLdzjMapper.insertProLdzj(proLdzj);

        // 添加埋点
        ProUser proUser = proUserMapper.selectProUserByUserId(proLdzj.getUserId());
        if (proUser == null) {
            throw new ServiceException("未查询到此学生信息");
        }
        BurialPoint burialPoint = BurialPoint.builder()
                .userId(proLdzj.getUserId())
                .type(LaborEnum.LABOR_SUMMARY_REPORT.getValue())
                .businessId(proLdzj.getId())
                .xyId(proUser.getXyId())
                .build();
        burialPointService.insertBurialPoint(burialPoint);

        return row;
    }

    public int pass(ProLdzj proLdPram){
        ProLdzj proLd = proLdzjMapper.selectProLdzjById(proLdPram.getId());
        ProLdFs proLdFs = proLdFsMapper.selectProLdFsById(proLdPram.getFsId());
        if (proLdFs==null)
            throw new ServiceException("请选择正确的审批分数");
        proLdPram.setStatus("2");
        proLdPram.setDfCount(proLdFs.getContent());
        proLdPram.setMarkNumber(proLdFs.getName());
        proLdPram.setOpTime(new Date());


        return proLdzjMapper.updateProLdzj(proLdPram);
    }

    public int reject(ProLdzj proLd){
        proLd.setStatus("3");
        proLd.setOpTime(new Date());
        return proLdzjMapper.updateProLdzj(proLd);
    }

    @Override
    public int repulse(ProLdzj proLd) {
        ProLdzj proLdzj = new ProLdzj();

        proLdzj.setId(proLd.getId());
        proLdzj.setStatus("3");
        proLdzj.setOpTime(new Date());
        return proLdzjMapper.updateRepulse(proLdzj);
    }
}
